#include <stdio.h>
#include <string.h>


#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"


#include "usbh_vcp.h"
#include "key.h"



namespace {

const char *TAG = "VCP_MAIN";


static bool handle_usb_rx(const uint8_t *data, size_t data_len, void *arg)
{
    printf("%.*s", data_len, data);
    return true;
}

void Key_Task(void *arg)
{
    while (1)
    {
        if (Key_Read_State() == KEY_STATE_PRESS)
        {
            ESP_LOGI(TAG, "KEY PRESS");

            char send[] = "VCP-Send-123\n";
            usbh_vcp_send((uint8_t*)send, strlen(send));
        }

        vTaskDelay(pdMS_TO_TICKS(10));
    }
    vTaskDelete(NULL);
}


extern "C" void app_main(void)
{
    Key_Init();
    xTaskCreatePinnedToCore(Key_Task, "Key_Task", 3*1024, NULL, 10, NULL, 1);

    ESP_LOGI(TAG, "Setting up VCP");
    usbh_vcp_setup(&handle_usb_rx);

    while (1)
    {
        usbh_vcp_loop();
    }
}

}
